package com.hdsx.netty.tomcatserver.netty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.HttpRequest;

import java.util.Map;

/**
 * @author 占贤银
 * time 2024/8/6 21:56
 * description 生命之灯因热情而点燃，生命之舟因拼搏而前行
 */
public class GPTomcatHandler extends ChannelInboundHandlerAdapter {

    private Map<String, XYServlet> handlerMappings;

    public GPTomcatHandler(Map<String, XYServlet> handlerMappings) {
        this.handlerMappings = handlerMappings;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        if (msg instanceof HttpRequest) {
            HttpRequest req = (HttpRequest) msg;
            XYRequest request = new XYRequest(ctx, req);
            XYResponse response = new XYResponse(ctx, req);
            if (handlerMappings.containsKey(request.getUrl())) {
                handlerMappings.get(request.getUrl()).service(request, response);
            } else {
                response.write("没有找到页面，请稍后重试！");
            }
        }
    }
}
